#include "ttClientProxy.h"
#include "myUtil.h"
#include "ttPack.h"
#include "autoLock.h"
#include "ttClientPrxMgr.h"
#include "ttServer.h"
#include "Me.h"
#include "ttMsgMap.h"

ttClientProxy::ttClientProxy()
{}

ttClientProxy::ttClientProxy(SOCKET s) : IttClient(s)
{
	myUtil::myCreateThread(ttClientProxy::recvWorkThread, this);
	myUtil::myCreateThread(ttClientProxy::sendWorkThread, this);
}

ttClientProxy::~ttClientProxy()
{}

int
ttClientProxy::move(float speed)
{
	{
		autoLock lock(ttClientPrxMgr::Get()->m_cs);
		for (ttClientPrxMgr::THISLIST::iterator iter = ttClientPrxMgr::Get()->getList().begin(); iter != ttClientPrxMgr::Get()->getList().end(); ++ iter){
			if (this->getID() != (*iter)->getID()){
				pttpack ppack = new ttpack;
				ppack->flag = ttpackflag::TP_CLIENTPRXMOVE;
				ppack->id = this->getID();
				ppack->x = this->getX();
				ppack->y = this->getY();
                ppack->bUDMoving = this->getUDMoving();
                ppack->bLRMoving = this->getLRMoving();
                ppack->btnUDDirection = this->getBtnUDDir();
                ppack->btnLRDirection = this->getBtnLRDir();
				ppack->direction = this->getDirection();
				ppack->speed = speed;
				{
					autoLock  lock1((*iter)->m_cs);
					(*iter)->push_back(ppack);
				}
			}
		}
	}
	__super::move(speed);
	return 0;
}

int
ttClientProxy::speak(WCHAR *sentence)
{
	autoLock lock(ttClientPrxMgr::Get()->m_cs);
	for (ttClientPrxMgr::THISLIST::iterator iter = ttClientPrxMgr::Get()->getList().begin(); iter != ttClientPrxMgr::Get()->getList().end(); ++ iter){
		if (this->getID() != (*iter)->getID()){
			pttpack ppack = new ttpack;
			ppack->flag = ttpackflag::TP_CLIENTPRXSPEAK;
			ppack->id = this->getID();
			wsprintf(ppack->sentence, L"%s", sentence);
			wsprintf(ppack->name, L"%s", this->getName());
			{
				autoLock lock1((*iter)->m_cs);
				(*iter)->push_back(ppack);
			}
		}
	}
		
	return 0;
}

DWORD WINAPI
ttClientProxy::recvWorkThread(LPVOID pParam)
{
	myUtil::testoutput("recvWork");
	ttClientProxy *pPrx = (ttClientProxy *)pParam;
	while (bRunning){
		pttpack ppack = new ttpack;
        myUtil::Recved(pPrx->getS(), (char *)ppack, sizeof(ttpack));
		
		autoLock lock(pPrx->m_cs);
		pPrx->push_back(ppack);
	}
	return 0;
}

DWORD WINAPI
ttClientProxy::sendWorkThread(LPVOID pParam)
{
	myUtil::testoutput("sendWork");
	ttClientProxy *pPrx = (ttClientProxy *)pParam;
	
	while (bRunning){
		pttpack ppack = NULL;
		{
			autoLock lock(pPrx->m_cs);
			if (!pPrx->isEmpty()){
				ppack = pPrx->getfront();
			}
		}
		if (NULL != ppack){
			answer(pPrx, ppack);
		}
	}
	return 0;
}

int
ttClientProxy::answer(ttClientProxy *pPrx, pttpack ppack)
{
	switch (ppack->flag)
	{
	case ttpackflag::TP_NAME:
		{
			myUtil::testoutput("TP_NAME");
			pPrx->setName(ppack->name);
			pPrx->setID(IDMaker::Get());
			pPrx->setXY(ppack->x, ppack->y);
            pPrx->setBtnUDDir(ppack->btnUDDirection);
            pPrx->setBtnLRDir(ppack->btnLRDirection);
			pPrx->setDirection(ppack->direction);
            pPrx->setUDMoving(ppack->bUDMoving);
            pPrx->setLRMoving(ppack->bLRMoving);
			ppack->flag = ttpackflag::TP_ID;
			ppack->id = pPrx->getID();
			myUtil::Sended(pPrx->getS(), (char *)ppack, sizeof(ttpack));

			ppack->flag = ttpackflag::TP_GETALL;
			{
				autoLock lock(ttClientPrxMgr::Get()->m_cs);
				for (ttClientPrxMgr::THISLIST::iterator iter = ttClientPrxMgr::Get()->getList().begin(); iter != ttClientPrxMgr::Get()->getList().end(); ++ iter){
					if (pPrx->getID() != (*iter)->getID()){
						myUtil::Sended((*iter)->getS(), (char *)ppack, sizeof(ttpack));
					}
				}
			}
			break;
		}
	case ttpackflag::TP_GETALL:
		{
			myUtil::testoutput("TP_GETALL");
			
			ppack->flag = ttpackflag::TP_GETALL;
			{
				autoLock lock(ttClientPrxMgr::Get()->m_cs);
				for (ttClientPrxMgr::THISLIST::iterator iter = ttClientPrxMgr::Get()->getList().begin(); iter != ttClientPrxMgr::Get()->getList().end(); ++ iter){
					if (pPrx->getID() != (*iter)->getID()){
						ppack->id = (*iter)->getID();
						wsprintf(ppack->name, L"%s", (*iter)->getName());
						ppack->x = (*iter)->getX();
						ppack->y = (*iter)->getY();

						ppack->btnUDDirection = (*iter)->getBtnUDDir();
						ppack->btnLRDirection = (*iter)->getBtnLRDir();
						ppack->direction = (*iter)->getDirection();
                        ppack->bUDMoving = (*iter)->getUDMoving();
                        ppack->bLRMoving = (*iter)->getLRMoving();
						myUtil::Sended(pPrx->getS(), (char *)ppack, sizeof(ttpack));
					}
				}
			}
			ppack->id = Me->getID();
			wsprintf(ppack->name, L"%s", Me->getName());
			ppack->x = Me->getX();
			ppack->y = Me->getY();

			
            ppack->btnUDDirection = Me->getBtnUDDir();
            ppack->btnLRDirection = Me->getBtnLRDir();
			ppack->direction = Me->getDirection();
            ppack->bUDMoving = Me->getUDMoving();
            ppack->bLRMoving = Me->getLRMoving();
			myUtil::Sended(pPrx->getS(), (char *)ppack, sizeof(ttpack));
			
			break;
		}
	case ttpackflag::TP_CLIENTMOVE:
		{
			pPrx->setXY(ppack->x, ppack->y);
			pPrx->setBtnUDDir(ppack->btnUDDirection);
			pPrx->setBtnLRDir(ppack->btnLRDirection);
			pPrx->setDirection(ppack->direction);
			pPrx->setUDMoving(ppack->bUDMoving);
            pPrx->setLRMoving(ppack->bLRMoving);
			pPrx->move(ppack->speed);
			break;
		}
	case ttpackflag::TP_CLIENTPRXMOVE:
		{
			myUtil::Sended(pPrx->getS(), (char *)ppack, sizeof(ttpack));
			break;
		}
	case ttpackflag::TP_CLIENTPRXSPEAK:
		{
			myUtil::Sended(pPrx->getS(), (char *)ppack, sizeof(ttpack));
			break;
		}
	case ttpackflag::TP_CLIENTSPEAK:
		{
			ttMsgMap::_msgloopvar::pmsg pMsg = new ttMsgMap::_msgloopvar::msg;
			wsprintf(pMsg->buf, L"%s : %s", pPrx->getName(), ppack->sentence);
			{
				autoLock lock(ttMsgMap::Get()->m_msglpvar.m_csMsgList);
				ttMsgMap::Get()->m_msglpvar.m_msglist.push_back(pMsg);
			}
			pPrx->speak(ppack->sentence);
			break;
		}
	}
	delete ppack;
	return 0;
}